<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/hexo/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/hexo/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/hexo/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/hexo/images/logo.svg" color="#222">

<link rel="stylesheet" href="/hexo/css/main.css">


<link rel="stylesheet" href="/hexo/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"youchen12138.gitee.io","root":"/hexo/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="种类 标签选择器  类选择器  ID选择器  全局选择器  群组选择器  后代选择器   伪类选择器 伪类选择器定义特殊状态下的样式  无法用标签、id、class及其他属性实现   链接伪类链接的4种状态：激活状态，已访问状态，未访问状态和鼠标悬停状态    伪类 说明    :link 未访问的链接   :visited 已访问的链接   :hover 鼠标悬停状态   :active 激活的">
<meta property="og:type" content="article">
<meta property="og:title" content="CSS基础知识点--选择器">
<meta property="og:url" content="https://youchen12138.gitee.io/hexo/2020/08/26/CSS%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E7%82%B9-%E9%80%89%E6%8B%A9%E5%99%A8/index.html">
<meta property="og:site_name" content="幽辰的博客">
<meta property="og:description" content="种类 标签选择器  类选择器  ID选择器  全局选择器  群组选择器  后代选择器   伪类选择器 伪类选择器定义特殊状态下的样式  无法用标签、id、class及其他属性实现   链接伪类链接的4种状态：激活状态，已访问状态，未访问状态和鼠标悬停状态    伪类 说明    :link 未访问的链接   :visited 已访问的链接   :hover 鼠标悬停状态   :active 激活的">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-08-26T06:23:21.000Z">
<meta property="article:modified_time" content="2020-08-26T06:26:53.952Z">
<meta property="article:author" content="幽辰">
<meta property="article:tag" content="CSS">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://youchen12138.gitee.io/hexo/2020/08/26/CSS%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E7%82%B9-%E9%80%89%E6%8B%A9%E5%99%A8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>CSS基础知识点--选择器 | 幽辰的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/hexo/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">幽辰的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/hexo/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/hexo/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags<span class="badge">23</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/hexo/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/hexo/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives<span class="badge">127</span></a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/08/26/CSS%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E7%82%B9-%E9%80%89%E6%8B%A9%E5%99%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          CSS基础知识点--选择器
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-08-26 14:23:21 / Modified: 14:26:53" itemprop="dateCreated datePublished" datetime="2020-08-26T14:23:21+08:00">2020-08-26</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/CSS/" itemprop="url" rel="index"><span itemprop="name">CSS</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="种类"><a href="#种类" class="headerlink" title="种类"></a>种类</h2><ul>
<li><p>标签选择器</p>
</li>
<li><p>类选择器</p>
</li>
<li><p>ID选择器</p>
</li>
<li><p>全局选择器</p>
</li>
<li><p>群组选择器</p>
</li>
<li><p>后代选择器</p>
</li>
</ul>
<h2 id="伪类选择器"><a href="#伪类选择器" class="headerlink" title="伪类选择器"></a>伪类选择器</h2><ul>
<li><p>伪类选择器定义特殊状态下的样式</p>
</li>
<li><p>无法用标签、id、class及其他属性实现</p>
</li>
</ul>
<h3 id="链接伪类"><a href="#链接伪类" class="headerlink" title="链接伪类"></a>链接伪类</h3><p>链接的4种状态：激活状态，已访问状态，未访问状态和鼠标悬停状态</p>
<table>
<thead>
<tr>
<th>伪类</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>:link</td>
<td>未访问的链接</td>
</tr>
<tr>
<td>:visited</td>
<td>已访问的链接</td>
</tr>
<tr>
<td>:hover</td>
<td>鼠标悬停状态</td>
</tr>
<tr>
<td>:active</td>
<td>激活的链接</td>
</tr>
</tbody></table>
<h4 id="链接伪类的顺序"><a href="#链接伪类的顺序" class="headerlink" title="链接伪类的顺序"></a>链接伪类的顺序</h4><p>:link &gt; :visited &gt; :hover &gt; :active</p>
<p>说明：</p>
<ol>
<li><p>a:hover必须置于a:link和a:visited之后，才有效</p>
</li>
<li><p>a:active必须置于a:hover之后，才有效</p>
</li>
<li><p>伪类名称对大小写不敏感</p>
</li>
</ol>
<h3 id="伪类-hover和-active"><a href="#伪类-hover和-active" class="headerlink" title="伪类:hover和:active"></a>伪类:hover和:active</h3><ol>
<li><p>:hover用于访问的鼠标经过某个元素时</p>
</li>
<li><p>:active用于一个元素被激活时（即按下鼠标之后放开鼠标之前的时间）</p>
</li>
</ol>
<h4 id="伪类-hover和-active兼容"><a href="#伪类-hover和-active兼容" class="headerlink" title="伪类:hover和:active兼容"></a>伪类:hover和:active兼容</h4><ul>
<li><p>IE6及更早版本，支持<code>&lt;a&gt;</code>元素的4种状态</p>
</li>
<li><p>IE6浏览器不支持元素的:hover和:active</p>
</li>
</ul>
<h2 id="CSS层叠"><a href="#CSS层叠" class="headerlink" title="CSS层叠"></a>CSS层叠</h2><ul>
<li><p>可以定义多个样式</p>
</li>
<li><p>不冲突时，多个样式课层叠为一个</p>
</li>
<li><p>冲突时，按不同样式规则优先级来应用样式</p>
</li>
</ul>
<h2 id="CSS选择器优先级"><a href="#CSS选择器优先级" class="headerlink" title="CSS选择器优先级"></a>CSS选择器优先级</h2><p>id选择器 &gt; class选择器 &gt; 标签选择器 &gt; 通配符</p>
<p>同一元素使用相同类型的选择器定义相同类型样式，优先级按就近原则</p>
<h2 id="CSS优先级规则"><a href="#CSS优先级规则" class="headerlink" title="CSS优先级规则"></a>CSS优先级规则</h2><p>同一样式表中：</p>
<ol>
<li>权值相同</li>
</ol>
<ul>
<li>就近原则（离被设置元素越近优先级越高）</li>
</ul>
<ol start="2">
<li>权值不同</li>
</ol>
<ul>
<li><p>根据权值来判断CSS样式</p>
</li>
<li><p>哪种CSS样式权值越高，就使用哪种样式</p>
</li>
</ul>
<h2 id="选择器权值"><a href="#选择器权值" class="headerlink" title="选择器权值"></a>选择器权值</h2><table>
<thead>
<tr>
<th>选择器</th>
<th>权值</th>
</tr>
</thead>
<tbody><tr>
<td>标签选择器</td>
<td>1</td>
</tr>
<tr>
<td>类选择器</td>
<td>10</td>
</tr>
<tr>
<td>伪类</td>
<td>10</td>
</tr>
<tr>
<td>ID选择器</td>
<td>100</td>
</tr>
<tr>
<td>通配符选择器</td>
<td>0</td>
</tr>
<tr>
<td>行内样式</td>
<td>1000</td>
</tr>
</tbody></table>
<h3 id="权值规则"><a href="#权值规则" class="headerlink" title="权值规则"></a>权值规则</h3><ol>
<li><p>统计不同选择器的个数</p>
</li>
<li><p>每类选择器的个数乘以相应权值</p>
</li>
<li><p>把所有的值相加得出选择器的权值</p>
</li>
</ol>
<h3 id="important规则"><a href="#important规则" class="headerlink" title="!important规则"></a>!important规则</h3><ul>
<li><p>可调整样式规则的优先级，使其权值最高</p>
</li>
<li><p>添加在样式规则之后，中间用空格隔开</p>
</li>
</ul>
<h2 id="CSS样式命名规则"><a href="#CSS样式命名规则" class="headerlink" title="CSS样式命名规则"></a>CSS样式命名规则</h2><ul>
<li><p>采用英文字母、数字以及”-“和”_”命名</p>
</li>
<li><p>以小写字母开头，不能以数字和”-“、”_”开头</p>
</li>
<li><p>命名形式：单字，连字符，下划线和驼峰</p>
</li>
<li><p>使用有意义命名</p>
</li>
</ul>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/hexo/tags/CSS/" rel="tag"># CSS</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/hexo/2020/08/26/CSS%E8%BF%9B%E9%98%B6%E7%9F%A5%E8%AF%86%E7%82%B9-2D%E8%BD%AC%E6%8D%A2/" rel="prev" title="CSS进阶知识点--2D转换">
      <i class="fa fa-chevron-left"></i> CSS进阶知识点--2D转换
    </a></div>
      <div class="post-nav-item">
    <a href="/hexo/2020/08/26/CSS%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86%E7%82%B9-%E7%BD%91%E9%A1%B5%E5%B8%83%E5%B1%80/" rel="next" title="CSS基础知识点--网页布局">
      CSS基础知识点--网页布局 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%A7%8D%E7%B1%BB"><span class="nav-number">1.</span> <span class="nav-text">种类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BC%AA%E7%B1%BB%E9%80%89%E6%8B%A9%E5%99%A8"><span class="nav-number">2.</span> <span class="nav-text">伪类选择器</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%93%BE%E6%8E%A5%E4%BC%AA%E7%B1%BB"><span class="nav-number">2.1.</span> <span class="nav-text">链接伪类</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%93%BE%E6%8E%A5%E4%BC%AA%E7%B1%BB%E7%9A%84%E9%A1%BA%E5%BA%8F"><span class="nav-number">2.1.1.</span> <span class="nav-text">链接伪类的顺序</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BC%AA%E7%B1%BB-hover%E5%92%8C-active"><span class="nav-number">2.2.</span> <span class="nav-text">伪类:hover和:active</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BC%AA%E7%B1%BB-hover%E5%92%8C-active%E5%85%BC%E5%AE%B9"><span class="nav-number">2.2.1.</span> <span class="nav-text">伪类:hover和:active兼容</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#CSS%E5%B1%82%E5%8F%A0"><span class="nav-number">3.</span> <span class="nav-text">CSS层叠</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#CSS%E9%80%89%E6%8B%A9%E5%99%A8%E4%BC%98%E5%85%88%E7%BA%A7"><span class="nav-number">4.</span> <span class="nav-text">CSS选择器优先级</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#CSS%E4%BC%98%E5%85%88%E7%BA%A7%E8%A7%84%E5%88%99"><span class="nav-number">5.</span> <span class="nav-text">CSS优先级规则</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%80%89%E6%8B%A9%E5%99%A8%E6%9D%83%E5%80%BC"><span class="nav-number">6.</span> <span class="nav-text">选择器权值</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9D%83%E5%80%BC%E8%A7%84%E5%88%99"><span class="nav-number">6.1.</span> <span class="nav-text">权值规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#important%E8%A7%84%E5%88%99"><span class="nav-number">6.2.</span> <span class="nav-text">!important规则</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#CSS%E6%A0%B7%E5%BC%8F%E5%91%BD%E5%90%8D%E8%A7%84%E5%88%99"><span class="nav-number">7.</span> <span class="nav-text">CSS样式命名规则</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="幽辰"
      src="/hexo/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">幽辰</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/hexo/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/hexo/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/hexo/tags/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">幽辰</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/hexo/lib/anime.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.ui.min.js"></script>

<script src="/hexo/js/utils.js"></script>

<script src="/hexo/js/motion.js"></script>


<script src="/hexo/js/schemes/pisces.js"></script>


<script src="/hexo/js/next-boot.js"></script>




  















  

  

</body>
</html>
